package com.kasax.skinchange

import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView

class UseThemeActivity : AppCompatActivity() {
    private lateinit var llContainer: LinearLayout
    private var titleCount: Int = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if ("default" != getSp(this, "theme")) {
            setTheme(R.style.Theme_Style1)
        }
        setContentView(R.layout.activity_use_theme)
        setTitle("使用主题换肤")

        llContainer = findViewById<LinearLayout>(R.id.ll_container)
    }

    fun handleReplaceChange(view: View) {
        toggleTheme()
        // 重新创建
        recreate()
    }

    private fun toggleTheme() {
        if ("default" == getSp(this, "theme")) {
            putSp(this, "theme", "")
        } else {
            putSp(this, "theme", "default")
        }
    }

    fun handleSlienceChange(view: View) {
        toggleTheme()
        val intent = intent
        // 去除动画
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
        finish()
        overridePendingTransition(0, 0) // 不设置进入退出动画
        startActivity(intent)
        overridePendingTransition(0, 0) // 不设置进入退出动画

    }

    fun handleDynamicAddTxt(view: View) {
        val textView = TextView(this)
        textView.text = "这是一个测试用的控件 Index: $titleCount"
        val textColor = if (titleCount % 2 == 0) {
            getThemeColor(this, R.attr.theme_sub_color, Color.BLACK)
        } else {
            getThemeColor2(this, R.attr.theme_sub_color, Color.BLACK)
        }
        textView.setTextColor(textColor)
        llContainer.addView(
            textView,
            ViewGroup.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT
            )
        )
        titleCount++
    }
}